package com.collect.zambia.zhiScene.zhiCollection.zhiGroup.zhiPerformance;

import android.annotation.SuppressLint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.collect.zambia.R;
import com.collect.zambia.zhiAdater.zhiLevelDetail.ZHILevelDetailMergeAmount;
import com.collect.zambia.zhiBase.BaseActivity;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRGroupBonusEntity;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRGroupPerformanceMoneyEntity;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRGroupPerformanceMoneyEntityV2;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRGroupUserEntity;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRGroupUserPerformanceMergeEntity;
import com.collect.zambia.zhiBean.zhiSend.ZHISPerfermanceEntity;
import com.collect.zambia.zhiGlobal.zhiAppSettings.ZHIProjectConfigs;
import com.collect.zambia.zhiGlobal.zhiAppSettings.ZHIProjectRouter;
import com.collect.zambia.zhiHelper.ZHIMainProcessHelper;
import com.collect.zambia.zhiScene.zhiCollection.zhiAdmin.zhiSort.ZHIAdminCompareByAmountASC;
import com.collect.zambia.zhiScene.zhiCollection.zhiAdmin.zhiSort.ZHIAdminCompareByAmountDES;
import com.collect.zambia.zhiUtils.StringUtil;
import com.collect.zambia.zhiUtils.ZHICalendarUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
@Route(path = ZHIProjectRouter.ZHI_ROOT_GROUP_B3_PERFORMANCE_AMOUNT)
public class ZHIPerformanceGroupAmountActivity extends BaseActivity<ZHIPerformanceGroupPresenter> implements ZHIPerformanceGroupContract.View {

    @BindView(R.id.srl_swipe_layout)
    SwipeRefreshLayout srl_swipe_layout;

    @BindView(R.id.tv_page_title)
    TextView tv_page_title;

    @BindView(R.id.tv_cumulative_bonus)
    TextView tv_cumulative_bonus;

    @BindView(R.id.tv_cumulative_bonus_date)
    TextView tv_cumulative_bonus_date;
    @BindView(R.id.iv_cumulative_bonus_date)
    ImageView iv_cumulative_bonus_date;

    @BindView(R.id.tv_real_time_data)
    TextView tv_real_time_data;
    @BindView(R.id.iv_real_time_data)
    ImageView iv_real_time_data;

    @BindView(R.id.tv_total_collected_amount)
    TextView tv_total_collected_amount;

    @BindView(R.id.rc_detail_list)
    RecyclerView rc_detail_list;

    @BindView(R.id.iv_sort)
    ImageView iv_sort;

    @BindView(R.id.iv_note)
    ImageView iv_note;

    private final ZHILevelDetailMergeAmount mZHILevelDetailMergeAmount = new ZHILevelDetailMergeAmount();

    private ArrayList<ZHIRGroupPerformanceMoneyEntity> mGroupPerformanceMoneyEntityArrayList;

    /**
     * 是否为降序
     */
    private boolean mIsArraySortDesNow = true;

    @Override
    protected void initInjector() {
        mActivityComponent.inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_perfermance_group_amount;
    }

    @Override
    protected void initView() {
        try {
            srl_swipe_layout.setColorSchemeColors(getResources().getColor(R.color.theme_color_0));

            tv_page_title.setOnClickListener(v -> ZHIProjectRouter.toPerformanceOrder());

            iv_note.setOnClickListener(v -> finish());

            iv_sort.setOnClickListener(v -> {
                if (mGroupPerformanceMoneyEntityArrayList != null) {
                    if (mIsArraySortDesNow) {
                        Collections.sort(mGroupPerformanceMoneyEntityArrayList, new ZHIAdminCompareByAmountASC());
                        mZHILevelDetailMergeAmount.notifyDataSetChanged();
                        mIsArraySortDesNow = false;
                    } else {
                        Collections.sort(mGroupPerformanceMoneyEntityArrayList, new ZHIAdminCompareByAmountDES());
                        mZHILevelDetailMergeAmount.notifyDataSetChanged();
                        mIsArraySortDesNow = true;
                    }
                }
            });

            iv_cumulative_bonus_date.setOnClickListener(v -> ZHIProjectRouter.toPerformanceGroupAmountHistory());
            tv_cumulative_bonus_date.setOnClickListener(v -> ZHIProjectRouter.toPerformanceGroupAmountHistory());

            tv_real_time_data.setOnClickListener(v -> ZHIProjectRouter.toPerformanceGroupRealTime());
            iv_real_time_data.setOnClickListener(v -> ZHIProjectRouter.toPerformanceGroupRealTime());

            initEvent();
            initData(true);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onGetErrorInfo() {
        srl_swipe_layout.setRefreshing(false);
    }

    @Override
    public void onGetGroupUserPerformanceOrderMerge(ArrayList<ZHIRGroupUserPerformanceMergeEntity> zhirGroupUserPerformanceMergeEntityArrayList, ZHISPerfermanceEntity zhisPerfermanceEntity) {
        srl_swipe_layout.setRefreshing(false);

    }

    @Override
    public void onGetGroupUserPerformanceMoneyMerge(ZHIRGroupPerformanceMoneyEntityV2 zhirGroupPerformanceMoneyEntityV2, ZHISPerfermanceEntity zhisPerfermanceEntity) {
        try {
            srl_swipe_layout.setRefreshing(false);

            ArrayList<ZHIRGroupPerformanceMoneyEntity> zhirGroupPerformanceMoneyEntityArrayList = zhirGroupPerformanceMoneyEntityV2.getList();

            BigDecimal resultDecimal = new BigDecimal("0");
            for (int i = 0; i < zhirGroupPerformanceMoneyEntityArrayList.size(); i++) {
                ZHIRGroupPerformanceMoneyEntity zhirGroupPerformanceMoneyEntity = zhirGroupPerformanceMoneyEntityArrayList.get(i);
                BigDecimal bigDecimal = zhirGroupPerformanceMoneyEntity.getRepayAmountBigDecimal();
                resultDecimal = resultDecimal.add(bigDecimal);
            }
            tv_total_collected_amount.setText(StringUtil.numberFormatter00(resultDecimal.stripTrailingZeros().toPlainString()));

            ///////////////////////
            for (int i = 0; i < zhirGroupPerformanceMoneyEntityArrayList.size(); i++) {
                ZHIRGroupPerformanceMoneyEntity zhirGroupPerformanceMoneyEntity = zhirGroupPerformanceMoneyEntityArrayList.get(i);
                zhirGroupPerformanceMoneyEntity.setEndDate(zhisPerfermanceEntity.getEndDateMonthDayFormat());
                zhirGroupPerformanceMoneyEntity.setStartDate(zhisPerfermanceEntity.getStartDateMonthDayFormat());
            }
            Collections.sort(zhirGroupPerformanceMoneyEntityArrayList, new ZHIAdminCompareByAmountDES());
            mGroupPerformanceMoneyEntityArrayList = zhirGroupPerformanceMoneyEntityArrayList;

            mZHILevelDetailMergeAmount.setNewData(zhirGroupPerformanceMoneyEntityArrayList);

            ///////////////////////
            if (mPresenter != null && ZHIProjectConfigs.SHOW_GROUP_BONUS) {
                mPresenter.getGroupBonus(getActivity(), getCaseBonusParams());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onGetGroupUsers(ArrayList<ZHIRGroupUserEntity> zhirGroupUserEntityArrayList) {
        try {
            srl_swipe_layout.setRefreshing(false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onGetGroupBonus(ZHIRGroupBonusEntity zhirGroupBonusEntity, ZHISPerfermanceEntity zhisPerfermanceEntity) {
        try {
            srl_swipe_layout.setRefreshing(false);

            tv_cumulative_bonus.setText(zhirGroupBonusEntity.getBonusFormat());
//            tv_cumulative_bonus_date.setText(zhisPerfermanceEntity.getEndDateForDisplay());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressLint({"InflateParams", "SetTextI18n"})
    public void initEvent() {
        srl_swipe_layout.setOnRefreshListener(() -> initData(false));

        mZHILevelDetailMergeAmount.setEmptyView(getRefreshEmptyView());

        rc_detail_list.setLayoutManager(new GridLayoutManager(this.getActivity(), 1));
        rc_detail_list.setAdapter(mZHILevelDetailMergeAmount);
    }

    public void initData(boolean showDialog) {
        ZHISPerfermanceEntity zhisPerfermanceEntity = getCaseSearchParams();
        assert mPresenter != null;
        mPresenter.getGroupUserPerformanceMoneyMerge(getActivity(), zhisPerfermanceEntity, showDialog);
    }

    public void refreshData() {
        initData(true);
    }

    private ZHISPerfermanceEntity getCaseSearchParams() {
        ZHISPerfermanceEntity zhisPerfermanceEntity = new ZHISPerfermanceEntity();
        zhisPerfermanceEntity.setLeven("B+");
        zhisPerfermanceEntity.setStartDateForDisplay(ZHICalendarUtil.getCurMonthFirstDayFormatString());
        zhisPerfermanceEntity.setEndDateForDisplay(ZHICalendarUtil.getCurMonthCurDayFormatString());
        zhisPerfermanceEntity.setStartDate(ZHICalendarUtil.getCurMonthFirstDayFormatString());
        zhisPerfermanceEntity.setEndDate(ZHICalendarUtil.getDateStringAfterDate(ZHICalendarUtil.getCurMonthCurDayFormatString(), 1));
        zhisPerfermanceEntity.setLeaderUserId(ZHIMainProcessHelper.getInstance().getLoginEntity().getUserId());
        return zhisPerfermanceEntity;
    }

    private ZHISPerfermanceEntity getCaseBonusParams() {
        ZHISPerfermanceEntity zhisPerfermanceEntity = new ZHISPerfermanceEntity();
        zhisPerfermanceEntity.setLeven("B+");
        zhisPerfermanceEntity.setStartDateForDisplay(ZHICalendarUtil.getCurMonthFirstDayFormatString());
        zhisPerfermanceEntity.setEndDateForDisplay(ZHICalendarUtil.getCurMonthCurDayFormatString());
        zhisPerfermanceEntity.setStartDate(ZHICalendarUtil.getCurMonthFirstDayFormatString());
        zhisPerfermanceEntity.setEndDate(ZHICalendarUtil.getDateStringAfterDate(ZHICalendarUtil.getCurMonthCurDayFormatString(), 1));
        zhisPerfermanceEntity.setLeaderUserId(ZHIMainProcessHelper.getInstance().getLoginEntity().getUserId());
        return zhisPerfermanceEntity;
    }

    private View getRefreshEmptyView() {
        View emptyView = getLayoutInflater().inflate(R.layout.view_empty_no_data, null);
        emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        emptyView.setOnClickListener(v -> initData(true));
        return emptyView;
    }
}
